/**
 * @Title:  AuthMechanismActivity.java 
 * @Copyright:  XXX Co., Ltd. Copyright YYYY-YYYY,  All rights reserved 
 * @Description:  TODO<请描述此文件是做什么的> 
 * @author:  MeiYi 
 * @data:  2016-1-5 上午11:24:06 
 * @version:  V1.0 
 */

package artid.com.arttogo.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.adapter.ExpressAdapter;
import artid.com.arttogo.bean.InfoExpress;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.ToastUtil;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;


/**
 * TODO<请描述这个类是干什么的> 艺术展厅
 * 
 * @author 熊彬
 * @data: 2016-1-5 上午11:24:06
 * @version: V1.0
 */
public class ExpressActivity extends BaseActivity {
	private Context mContext;
	private PullableListView mListView;
	private ExpressAdapter adapter;
	private List<InfoExpress.Info> list;
	PullToRefreshLayout mPullLayout;
	int pageIndex = 1;
	String pagesize = "20";
	int width;
	String orderid = "";
	private View mHeadView;
	private TextView orderNum;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_express);
		mContext = ExpressActivity.this;
		orderid = getIntent().getStringExtra("id");
		// 头布局
		mHeadView = View.inflate(this, R.layout.head_express, null);
		orderNum= (TextView) mHeadView.findViewById(R.id.head_express_orderNum);
		orderNum.setText("订单号："+getIntent().getStringExtra("orderNum"));
		mListView = (PullableListView) findViewById(R.id.listivew);
		mListView.addHeaderView(mHeadView);
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mTittle.setText("查看物流");
		mBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		loading = (RelativeLayout) findViewById(R.id.rel_progressbar);
		hint = (LinearLayout) findViewById(R.id.ll_hint);
		loading.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
			}
		});

		mPullLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				pageIndex = 1;
				requestGradeList();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
//				pageIndex++;
//				requestGradeList();
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
			}
		});
		requestGradeList();
	}

	View headView = null;

	private void requestGradeList() {
		if (TextUtils.isEmpty(orderid)) {
			ToastUtil.toast(mContext, "id为空");
			return;
		}
		VolleyBasic.getInstance(ExpressActivity.this).post(
				Cache.infoExpress,
				VolleyMap.getInstance().infoExpress(orderid), InfoExpress.class,
				new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						
						InfoExpress item = (InfoExpress) vd.getData();
						list = item.getList();
						
						if ((list.isEmpty()) ) {
							if (pageIndex==1) {
								mListView.setVisibility(View.INVISIBLE);
								hint.setVisibility(View.VISIBLE);
							}
							return;
						}
						hint.setVisibility(View.GONE);
						if (mListView.getVisibility() == View.INVISIBLE) {
							mListView.setVisibility(View.VISIBLE);
						}
						if (pageIndex != 1) {
							adapter.addList(list);
							return;
						}
						
						adapter = new ExpressAdapter(mContext, list);
						mListView.setAdapter(adapter);

					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}


}
